<?php

namespace platform\core;

use yii\base\Model;

class BaseModel extends Model
{
    public $accountType = 1;//1：系统创建；
    public $accountID = 0;
    public $isSuperman = 0;

    public $stores = [];
    public $storeIDs = "";

    public $sysid = 0;

    public function init()
    {
        parent::init();
        $account = \Cm::$app->user->getIdentity();
        $accountType = $account['type'];

        $managerInfo = \Cm::$db->one("SELECT * FROM sys_manager WHERE id={$account['id']}");
        $account['is_superman'] = $managerInfo['is_superman'];

        $this->accountType = $accountType;
        $this->accountID = $account['id'];
        $this->isSuperman = $managerInfo['is_superman'];

        $sys = \Cm::$db->one("SELECT * FROM sys WHERE alias='platform'");
        $this->sysid = empty($sys) ? 0 : $sys['id'];
    }

    /*
    *刷新订单
    */
    public function refreshorder(){
        $time = time();
        /*待支付的订单 更新已失效  1*/
        $this->changeorderstatus(1,$time,5);
        /*已支付的订单 更新已发车 3*/
        //$this->changeorderstatus(3,$time,3);
        /*已支付的订单 更新已完成 4*/  
        //$this->changeorderstatus(4,$time,4);
    }

    /*
    *更新订单状态
    */
    public function changeorderstatus($type,$time,$status){
        $setting = \Cm::$db->all("SELECT `value`,circuit_id FROM rule_setting WHERE type={$type} AND status=1");
        $timestr = date("Y-m-d H:i:s",$time);
        $where = $type == 1 ? " AND a.order_status=0 " : ($type==3?" AND a.order_status IN (1,2)" : " AND a.order_status IN (1,2,3) ");
        if(!empty($setting)){
            $settingarr = [];
            foreach ($setting as $key => $value) {
                $settingarr[$value['circuit_id']] = $value['value'];
            }
            $list = \Cm::$db->all("SELECT a.id,a.addtime,a.circuit_id,b.departuretime FROM `order` a 
                                LEFT JOIN user u ON u.id=a.user_id 
                                LEFT JOIN shift b ON b.id=a.shift_id 
                                WHERE 1=1 {$where} AND a.status=1");
            if(!empty($list)){
                foreach ($list as $key => $value) {
                    $setting_time = isset($settingarr[$value['circuit_id']]) ? $settingarr[$value['circuit_id']] : (isset($settingarr[0]) ? $settingarr[0] : 0);
                    $ordertime = $type==1 ? strtotime($value['addtime'])+$setting_time*60 : strtotime($value['departuretime'])+$setting_time*60;
                    if($setting_time != 0 && $ordertime<=$time){
                        $mdata = [
                            'order_status' => $status,
                            'updatetime' => $timestr
                        ];
                        \Cm::$db->update("order",$mdata," id={$value['id']}");
                    } 
                }
            }
        }
    }

    /*
    *首字母
    */
    public function allFirstchar(){
        return ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    }

    /*
    *所有省份
    */
    public function allProvince(){
        $province = \Cm::$db->all("SELECT serial_number,name FROM all_area WHERE level=1");
        return $province;
    }

    /*
    *所有地区
    */
    public function allArea(){
        $area = \Cm::$db->all("SELECT id,serial_number,name,province,city,short_name,pinyin,short_pinyin,firstchar,parentpath,level FROM all_area");
        $data = [];
        $serial_number1 = '';
        $serial_number2 = '';
        foreach ($area as $key => $value) {
            if($value['level'] == 1){
                $value['data'] = '';  
                if(in_array($value['serial_number'], [110000,120000,310000,500000])){
                    $value['data'] =  [$value['serial_number'] => $value];
                }  
                $serial_number1 = $value['serial_number'];
                $data[$serial_number1] = $value;
                continue;
            }
            if($value['level'] == 2){
                $data[$serial_number1]['data'] = empty($data[$serial_number1]['data']) ? [] : $data[$serial_number1]['data'];
                $value['data'] = "";
                if(!in_array($serial_number1, [110000,120000,310000,500000])){
                    $value['data'] = [$value['serial_number'] => $value];
                }
                $serial_number2 = $value['serial_number'];
                $data[$serial_number1]['data'][$serial_number2] = $value;
                continue;
            }
            if($value['level'] == 3){
                $data[$serial_number1]['data'][$serial_number2]['data'][$value['serial_number']] = $value;
                continue;    
            }
        }
        return $data;
    }

    /*
    * 所有开通的城市
    */
    public function allOpenCity(){
        $all = \Cm::$db->all("SELECT code,province,city,full_name,level,parentpath FROM open_city a WHERE status=1 ORDER BY code ASC");
        
        $data = [];
        $province = [
            'code' => 0,
            'province' => '',
            'city' => '',
            'full_name' => '',
            'level'=>1,
            'data'=> []
        ];
        $city = [
            'code' => 0,
            'province' => '',
            'city' => '',
            'full_name' => '',
            'level'=>2,
            'data'=> []
        ];
        foreach ($all as $key => $value) {
            $code = $value['code'];
            if($value['level'] == 1){
                $value['data'] = '';  
                if(in_array($code, [110000,120000,310000,500000])){
                    $value['data'] =  [$code => $value];
                }
                $data[$code] = $value;
                continue;
            }
            if($value['level'] == 2){
                $codearr = explode(",", $value['parentpath']);
                if(!isset($data[$codearr[0]])){
                    $data[$codearr[0]] = $province;
                    $data[$codearr[0]]['code'] = $codearr[0];
                    $data[$codearr[0]]['full_name'] = $value['province'];
                }
                if(in_array($codearr[0], [110000,120000,310000,500000])){
                    $value['data'] = '';
                }else{
                    $value['data'] =  [$code => $value];    
                }
                $data[$codearr[0]]['data'][$code] = $value;
                continue;
            }
            if($value['level'] == 3){
                $codearr = explode(",", $value['parentpath']);
                if(!isset($data[$codearr[0]])){
                    $data[$codearr[0]] = $province;
                    $data[$codearr[0]]['code'] = $codearr[0];
                    $data[$codearr[0]]['full_name'] = $value['province'];
                }
                if(!isset($data[$codearr[0]]['data'][$codearr[1]])){
                    $data[$codearr[0]]['data'][$codearr[1]] = $city;
                    $data[$codearr[0]]['data'][$codearr[1]]['code'] = $codearr[1];
                    $data[$codearr[0]]['data'][$codearr[1]]['full_name'] = $value['city'];
                }
                $data[$codearr[0]]['data'][$codearr[1]]['data'][$code] = $value;
            }
        }
        return $data;
    }

    /*
    * 所有开通的站点
    */
    public function allSite(){
        $all = \Cm::$db->all("SELECT s.id,s.full_name,s.city_code,oc.province,oc.city,oc.full_name as area,oc.level,oc.parentpath
            FROM site s
            LEFT JOIN open_city oc ON oc.code=s.city_code
            WHERE s.status=1 AND oc.status=1 ORDER BY s.city_code ASC");
        $data = [];
        $province = [
            'city_code' => 0,
            'province' => '',
            'city' => '',
            'full_name' => '',
            'level'=>1,
            'data'=> [],
            'site' => []
        ];
        $city = [
            'city_code' => 0,
            'province' => '',
            'city' => '',
            'full_name' => '',
            'level'=>2,
            'data'=> [],
            'site' => []
        ];
        foreach ($all as $key => $value) {
            $code = $value['city_code'];
            $site = [
                'code' => $value['city_code'],
                'siteid' => $value['id'],
                'sitename' => $value['full_name']
            ];
            if($value['level'] == 1){
                if(!isset($data[$code])){
                    $value['full_name'] = $value['area'];
                    $value['data'] = '';  
                    if(in_array($code, [110000,120000,310000,500000])){
                        $value['data'] =  [$code => $value];
                    }
                    $value['site'] = [];
                    $data[$code] = $value;
                }
                $data[$code]['site'][] = $site;
                continue;
            }
            if($value['level'] == 2){
                $codearr = explode(",", $value['parentpath']);
                if(!isset($data[$codearr[0]])){
                    $data[$codearr[0]] = $province;
                    $data[$codearr[0]]['city_code'] = $codearr[0];
                    $data[$codearr[0]]['full_name'] = $value['province'];
                }
                if(!isset($data[$codearr[0]]['data'][$code])){
                    $value['full_name'] = $value['area'];
                    if(in_array($codearr[0], [110000,120000,310000,500000])){
                        $value['data'] = '';
                    }else{
                        $value['data'] =  [$code => $value];    
                    }
                    $value['site'] = [];
                    $data[$codearr[0]]['data'][$code] = $value;
                }
                if(in_array($codearr[0], [110000,120000,310000,500000])){
                    $data[$codearr[0]]['data'][$code]['site'][] = $site; 
                }else{
                    $data[$codearr[0]]['data'][$code]['data'][$code]['site'][] = $site; 
                }    
                continue;
            }
            if($value['level'] == 3){
                $codearr = explode(",", $value['parentpath']);
                if(!isset($data[$codearr[0]])){
                    $data[$codearr[0]] = $province;
                    $data[$codearr[0]]['city_code'] = $codearr[0];
                    $data[$codearr[0]]['full_name'] = $value['province'];
                    $data[$codearr[0]]['site'] = [];
                }
                if(!isset($data[$codearr[0]]['data'][$codearr[1]])){
                    $data[$codearr[0]]['data'][$codearr[1]] = $city;
                    $data[$codearr[0]]['data'][$codearr[1]]['city_code'] = $codearr[1];
                    $data[$codearr[0]]['data'][$codearr[1]]['full_name'] = $value['city'];
                    $data[$codearr[0]]['data'][$codearr[1]]['site'] = [];
                }
                if(!isset($data[$codearr[0]]['data'][$codearr[1]]['data'][$code])){
                    $value['full_name'] = $value['area'];
                    $data[$codearr[0]]['data'][$codearr[1]]['data'][$code] = $value;
                }
                $data[$codearr[0]]['data'][$codearr[1]]['data'][$code]['site'][] = $site;
            }
        }
        return $data;
    }

    /*
    * 车辆类型
    */
    public function carModelsType(){
        $all = \Cm::$db->all("SELECT id,name FROM car_models_type WHERE status=1");
        return $all;
    }

    /*
    * 车辆座位类型
    */
    public  function  carSeatType(){
        $all = \Cm::$db->all("SELECT id,name FROM car_seat_type WHERE status=1");
        return $all;    
    }

    /*
    * 车型
    */
    public function carModels(){
        $all = \Cm::$db->all("SELECT id,name,c_seat,p_seat FROM car_models WHERE status=1");
        return $all;        
    }

    /*
    * 检票员
    */
    public function getInspector(){
        $all = \Cm::$db->all("SELECT id,username as name FROM sys_manager WHERE status=1 AND is_check=1");
        return $all;            
    }

    /*
    *所有的车
    */
    public function allCar($models_id=0){
        $where = " status=1 ";
        if($models_id!=0){
            $where .= " AND models_id={$models_id} ";
        }
        $all = \Cm::$db->all("SELECT id,p_seat,models_id,plate FROM car WHERE {$where}");
        return $all;
    }

    /*
    * 所有线路
    */
    public function allCircuit(){
        $all = \Cm::$db->all("SELECT id,name as name FROM circuit WHERE status=1 ORDER BY is_hot DESC,id ASC");
        return $all;                
    }

    /*
    *日志类型
    */
    public function authlogtype(){
        return [
            1=>'登录',
            2=>'退出',
            3=>'编辑管理员信息',
            4=>'新增管理员信息',
            5=>'删除管理员信息',
            6=>'变更权限',
            7=>'获取权限',
            8=>'刷新权限',
            9=>'新增角色',
            10=>'编辑角色',
            11=>'删除角色',
            12=>'编辑系统规则',
            13=>'编辑退改规则',
            14=>'新增退改规则',
            15=>'删除退改规则',
            16=>'起停用退改规则',
            17=>'删除会员',
            18=>'获取会员的常用联系人',
            19=>'删除车辆',
            20=>'新增车辆',
            21=>'编辑车辆',
            22=>'删除车型',
            23=>'新增车型',
            24=>'编辑车型',
            25=>'编辑站点',
            26=>'新增站点',
            27=>'删除站点',
            28=>'删除城市',
            29=>'新增城市',
            30=>'编辑城市',
            31=>'新增班次',
            32=>'编辑班次',
            33=>'新增线路',
            34=>'编辑线路',
            35=>'删除线路',
            36=>'查看线路标签',
            37=>'编辑线路标签',
            38=>'新增线路标签',
            39=>'删除线路标签',
            40=>'起停用线路',
            41=>'派车',
            42=>'检票',
            43=>'订单审核',
            44=>'查看订单审核记录',
            45=>'查看客户反馈意见',
            46=>'保存轮播图',
            47=>'删除轮播图',
            48=>'启停用轮播图',
            49=>'启停用班次',
        ];
    }

    /*
    *获取所有订单的车牌
    */
    public function getCarAllPlate(){
        $res = \Cm::$db->all("SELECT b.id,b.plate FROM order_car a INNER JOIN car b ON a.car_id=b.id GROUP BY b.id");
        return $res;
    }

    /*
    *获取所有订单班次的开始站点
    */
    public function getAllShiftSsite(){
        $res = \Cm::$db->all("SELECT c.id,c.name FROM `order` a LEFT JOIN circuit b ON b.id=a.circuit_id LEFT JOIN site c ON c.id=b.site_sid GROUP BY c.id");
        return $res;
    }

    /*
    *获取所有订单班次的结束站点
    */
    public function getAllShiftEsite(){
        $res = \Cm::$db->all("SELECT c.id,c.name FROM `order` a LEFT JOIN circuit b ON b.id=a.circuit_id LEFT JOIN site c ON c.id=b.site_eid GROUP BY c.id");
        return $res;    
    }

    /*
    *获取所有订单的路线
    */
    public function getAllCircuit(){
        $res = \Cm::$db->all("SELECT b.id,b.name FROM `order` a LEFT JOIN circuit b ON b.id=a.circuit_id GROUP BY b.id");
        return $res;
    }

    /*余票扣减*/
    public function checkSticket($circuit_id,$shift_id,$s_ticket){
        $res = \Cm::$db->one("SELECT `value` FROM rule_setting WHERE type=2 AND status=1 AND circuit_id={$circuit_id}");
        if(!empty($res)){
            if($res['value'] == 0){
                return $s_ticket;
            }else{
                $ordernum = \Cm::$db->one("SELECT count(b.id) AS num FROM `order` a LEFT JOIN order_passenger b ON a.id=b.order_id WHERE a.status=1 AND a.shift_id={$shift_id} AND a.order_status!=5 AND b.status=1");
                return $ordernum['num'] > $s_ticket ? 0 : $s_ticket-$ordernum['num'];
            }
        }else{
            $res = \Cm::$db->one("SELECT `value` FROM rule_setting WHERE type=2 AND status=1 AND circuit_id=0");
            if(!empty($res)){
                if($res['value'] == 0){
                    return $s_ticket;
                }else{
                    $ordernum = \Cm::$db->one("SELECT count(b.id) AS num FROM `order` a LEFT JOIN order_passenger b ON a.id=b.order_id WHERE a.status=1 AND a.shift_id={$shift_id} AND a.order_status!=5 AND b.status=1");
                    return $ordernum['num'] > $s_ticket ? 0 : $s_ticket-$ordernum['num'];
                }
            }else{
                return $s_ticket;
            }
        }
    }
}